package com.starry.core.common.domain;

import cn.hutool.http.HttpStatus;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

/**
 * @Description 统一响应类
 * @Author xiaoke
 *
 */
@Data
public class R<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private int code;

    /**
     * 提示信息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    /**
     * 接口是否处理成功
     */
    private Boolean success;

    public R() {
    }

    public R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public R(int code, String msg, T data, Boolean success) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.success = success;
    }

    public static <T> R<T> success() {
        return new R<>(HttpStatus.HTTP_OK, "操作成功", null, true);
    }

    public static <T> R<T> success(T data) {
        return new R<>(200, "操作成功", data, true);
    }

    public static <T> R<T> success(int code, T data) {
        return new R<>(code, "操作成功", data, true);
    }

    public static <T> R<T> fail() {
        return new R<>(500, "操作失败", null, false);
    }

    public static <T> R<T> fail(int code, String msg) {
        return new R<>(code, msg, null, false);
    }

    public static <T> R<T> fail(int code, String msg, T data) {
        return new R<>(code, msg, data, false);
    }
}
